/** 
* Define the main controller which manage current user, parameters, translations and menu behavior
*/
app.controller('mainController', ['$rootScope', '$parse', '$location', 'toolsServices', 'userServices', function($rootScope, $parse, $location, toolsServices, userServices) {
	
	$rootScope.currentUser = undefined;

	$rootScope.$on('$routeChangeStart', function(event, curr, prev) { 
		/*
		if (!userServices.isLoggedIn()) {
			console.log('User is denied');
			event.preventDefault();
			//TODO : Redirect to index and open authenticate or display message
			//$location.path('/login');
		}
		else {
			console.log('User is allow');
		}
		*/
		$('.preloader').show();
	});
	
	$rootScope.$on('$routeChangeSuccess', function(event, curr, prev) { 
		$('.preloader').hide();
	});
	
	$rootScope.$on('$routeChangeError', function () {
		toastr.error("Echec lors de la navigation");
	});
	
	var parametersLoader = $.getJSON("./../parameters.json", function() {
		})
		.done(function(json) {
			toolsServices.setParameters(json);
			console.debug('parameters file loaded');
		})
		.fail(function(parametersLoader, textStatus, error) {
			toastr.error("Echec lors de la récupération du fichier de paramétrage " + textStatus, "System");
			console.error("mainController load file parameters failed : " + error + " " + textStatus);
		})
		.always(function() {
			console.debug('load parameters file finished');
	});
	
	console.info('Start to configure translations');
	
	/**
	* Initialize the repository
	*/
	toolsServices.getTranslations (function (data) {
		console.debug('toolsServices initialize translations received : ' + data);
		$rootScope.translations = data;
		
		$.each(data, function(key, value) {
			console.log(key + ' => ' + value);
			var model = $parse(key);
			model.assign($rootScope, value);
		});
	 });
}]);
